﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityObjects;
using BAL.Controller;
using DAL_V1.App_Code.DBBroker;

namespace PresentationLayer.View.StoreClerk
{

    public partial class MakeStationeryAdjustment : System.Web.UI.Page
    {
            //for GridView Adjustment Items list
            private static List<AdjustmentCatalogue> AdjCatlist;
            private static List<Catalogue> catal_list;
            List<Catalogue> allCatal_list;
      
    protected void Page_Load(object sender, EventArgs e)
    {    
            if (!IsPostBack)
            {
                       List<Category> categ_list = new List<Category>();
                       catal_list = new List<Catalogue>();
                       allCatal_list = new List<Catalogue>();
            
                       DateTime today = DateTime.Now;
                       string adjDate = today.ToString("dd/MM/yyyy");
                       lblTodayDate.Text = adjDate;

                       if (Session["AdjCatlist"] != null) Session["AdjCatlist"] = null;
                     // {
                         AdjCatlist = new List<AdjustmentCatalogue>();
                    
                    //  } else AdjCatlist = (List<AdjustmentCatalogue>)Session["AdjCatlist"];

                 
                      categ_list = MakeStationeryAdjController.getMakeStationeryAdjController().selectAllCategory();
                      if (categ_list == null)
                      {
                             ddlCategory.DataSource = null;
                             ddlCategory.DataBind();
                      }
                     else
                      {
                            ddlCategory.DataSource = categ_list;
                            ddlCategory.DataTextField = "CategoryName";
                            ddlCategory.DataValueField = "CategoryId";
                            ddlCategory.DataBind();
                      }

                      //for All Catalogue
                      allCatal_list = MakeStationeryAdjController.getMakeStationeryAdjController().selectAllCatalogue();
                      if (allCatal_list == null)
                      {
                         Session["Allcatlist"]=null;
                      }
                      else
                      {
                         Session["Allcatlist"] = allCatal_list;
                      }

              }
      }

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
                 int sel = Convert.ToInt32(ddlCategory.SelectedValue);
                 catal_list = MakeStationeryAdjController.getMakeStationeryAdjController().selectCatalogueByCategoryId(sel);
                    
                 if (catal_list == null)
                 {
                        gvCatalist.DataSource = null;
                        gvCatalist.DataBind();
                 }
                 else
                 {
                        gvCatalist.DataSource = catal_list;
                        gvCatalist.DataBind();
                 }
                   
                 //Maintain user selected Catalogue List
                  ViewState["selCat"] = ddlCategory.SelectedItem.Text;
                  btnConfirm.Visible = true;
    }

    protected void gvCatalist_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
            btnConfirm.Visible = true;
            foreach (GridViewRow gvRow in gvCatalist.Rows)
            {
                if (gvRow.RowType == DataControlRowType.DataRow)
                {
                        string id = gvRow.Cells[1].Text;
                        TextBox txt = (TextBox)gvRow.FindControl("tbQty");
                        Control chkselect = (CheckBox)gvRow.FindControl("myCheckBox");
                        RadioButton rd1 = (RadioButton)gvRow.FindControl("rbDamage");
                        RadioButton rd2 = (RadioButton)gvRow.FindControl("rbGet");

                        bool chkresult = ((CheckBox)gvRow.FindControl("myCheckBox")).Checked;
                        bool DChk=((RadioButton)gvRow.FindControl("rbDamage")).Checked;
                        bool Gchk = ((RadioButton)gvRow.FindControl("rbGet")).Checked;


                        /* Maintain values inside control while Page Switching */
                        /*******************************************************/
                        if (chkresult) { ViewState.Add("CHECKED_ITEMS" + id, chkresult); }
                        if (!String.IsNullOrEmpty(txt.Text)) { ViewState.Add("Adj_QTY" + id, txt.Text); }
                        if (DChk) { ViewState.Add("Damage_ITEMS" + id, DChk); }
                        if (Gchk) { ViewState.Add("Get_ITEMS" + id, Gchk); } 

                 } 
              }
                        /* Gridview Rebinding for new Page */
                        /*=================================*/
                        gvCatalist.PageIndex = e.NewPageIndex;
                        gvCatalist.DataSource = catal_list;
                        gvCatalist.DataBind();
                      
    }

    protected void myCheckBox_CheckedChanged(object sender, EventArgs e)
    {            
                       btnConfirm.Visible = true;
                       bool rdflag = false;
                       bool Qty_invalid=false;


                       CheckBox thischkbox = (CheckBox)sender;
                       GridViewRow thisGridViewRow = (GridViewRow)thischkbox.Parent.Parent;
                       RadioButton rd1 = (RadioButton)thisGridViewRow.Cells[4].FindControl("rbDamage");
                       RadioButton rd2 = (RadioButton)thisGridViewRow.Cells[4].FindControl("rbGet");
                       TextBox txtbox=((TextBox)thisGridViewRow.Cells[3].FindControl("tbQty"));
                       string textBoxText = txtbox.Text;


                       /*Data insert into AdjustmentItem-object*/
                       AdjustmentCatalogue adCatObj=new AdjustmentCatalogue();
                       adCatObj.ItemId = thisGridViewRow.Cells[1].Text;
                       adCatObj.ItemName = thisGridViewRow.Cells[2].Text;
  
                       if (rd1.Checked == true)
                       {
                           rdflag = true;
                           adCatObj.Reason = "Damage";
                       }
                       else if (rd2.Checked == true)
                       {
                           rdflag = true;
                           adCatObj.Reason ="Get";
                       }

                       if (rdflag == false)
                       {
                           lblmsg.Visible = true;
                           lblmsg.Text = "Adjustment Reason and Quantity are mandatory for every item.";
                           thischkbox.Checked = false;
                       }
                    
                       if (textBoxText != "")
                       {
                           adCatObj.AdjQty = Convert.ToInt32(textBoxText);
                           
                          if (catal_list.Count != 0)
                          {
                               Catalogue myCatObj = catal_list.Find(delegate(Catalogue cp) { return cp.ItemId == adCatObj.ItemId ; });
                               if (adCatObj.AdjQty> myCatObj.TotalQty)
                               {
                                   Qty_invalid=true;
                                   string tQty = Convert.ToString (myCatObj.TotalQty);
                                   lblmsg.Visible = true;
                                   lblmsg.Text = "Existing Total Quantity ("+tQty+") is less than Adjusted Quantity.";
                                   thischkbox.Checked = false;
                               }
                           }
                       }

                      else if (textBoxText == "")
                      {
                           lblmsg.Visible = true;
                           lblmsg.Text = "Adjustment Reason and Quantity are mandatory for every item."; 
                           thischkbox.Checked = false;

                      }

                     /*user Added-Items into ProcessItem list*/
                       if (rdflag == true && textBoxText != "" && Qty_invalid==false)
                     {      
                              lblmsg.Visible = false;

                              if (thischkbox.Checked)
                              {
                                 AdjCatlist.Add(adCatObj);
                              }
                                 
                             else
                             {
                                 /*If user added same items again */
                                  AdjustmentCatalogue obj = new AdjustmentCatalogue();
                                  for (int i = 0; i < AdjCatlist.Count; i++)
                                  {
                                     if( AdjCatlist[i].ItemId==adCatObj.ItemId)
                                         AdjCatlist.Remove(AdjCatlist[i]);
                                  }
                             }
                       }
                     
                         /*If user remove previous selected-item then remove from ProcessItem list*/
                         if (!thischkbox.Checked)
                         {
                                bool chkresult=false;
                                string id = thisGridViewRow.Cells[1].Text;
                                ViewState.Add("CHECKED_ITEMS" + id, chkresult); 
                         }
             
    }

    protected void gvCatalist_RowDataBound(object sender, GridViewRowEventArgs e)
    {
                    string textboxValue = "";
                    bool chkr=false;
                    bool Dchk=false;
                    bool Gchk=false;
              
                    if (e.Row.RowType == DataControlRowType.DataRow) 
                    {
                            string id = e.Row.Cells[1].Text;
                            if (ViewState["Adj_QTY" + id] != null) { textboxValue = (String)ViewState["Adj_QTY" + id]; }
                            if (ViewState["CHECKED_ITEMS" + id] != null)
                            { chkr = (bool)ViewState["CHECKED_ITEMS" + id]; }
                            if (ViewState["Damage_ITEMS" + id] != null) { Dchk = true; }
                            if (ViewState["Get_ITEMS" + id] != null) { Gchk = true; }
               

                            if (textboxValue != "")
                            {
                                     TextBox txt = (TextBox)e.Row.FindControl("tbQty");
                                     txt.Text = (string)textboxValue; 
                             }
                             if (chkr)
                             {
                                     CheckBox tmpChkBox = (CheckBox)e.Row.FindControl("myCheckBox");
                                     tmpChkBox.Checked = true;
                      
                              }
                              if (Dchk)
                              {
                                    RadioButton tmpRd1 = (RadioButton)e.Row.FindControl("rbDamage");
                                    tmpRd1.Checked = true;
                              }
                              if (Gchk)
                              {
                                    RadioButton tmpRd2 = (RadioButton)e.Row.FindControl("rbGet");
                                    tmpRd2.Checked = true;

                              }

	                }

       }

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
                  //necessary data to process
                  string SelCat = ViewState["selCat"].ToString();
                  if (AdjCatlist.Count != 0)
                  {
                          Session["AdjCatlist"] = AdjCatlist;
                          Session["SelCat"] = SelCat;
                  }
                            
                  Response.Redirect("AdjustmentSubmit.aspx");
       
     }

    protected void rbGet_CheckedChanged(object sender, EventArgs e)
    {
                    RadioButton rbGet = (RadioButton)sender;
                    GridViewRow thisGridViewRow = (GridViewRow)rbGet.Parent.Parent;
                    string id = thisGridViewRow.Cells[1].Text;
                           
                    string updatedata = "";
                    if (rbGet.Checked) updatedata = "Get";

                    Edit_AdjustmentList(id,rbGet ,updatedata);

    }
    
    /*If user update "Reason" after added into List  */
    protected void rbDamage_CheckedChanged(object sender, EventArgs e)
    {
                  
                  RadioButton rbDamage = (RadioButton)sender;
                  GridViewRow thisGridViewRow = (GridViewRow)rbDamage.Parent.Parent;
                  string id = thisGridViewRow.Cells[1].Text;
                             
                  string updatedata = "";
                  if (rbDamage.Checked) updatedata = "Damage";
                  Edit_AdjustmentList(id, rbDamage, updatedata);
    }
              
    /*If user update Adjustment Quantity and Reason after added into List  */
    private static void Edit_AdjustmentList(string id,WebControl control,string updatedata)
    {
              String cTypeId = control.ID.ToString();
              AdjustmentCatalogue adCatObj = new AdjustmentCatalogue();

              for (int i = 0; i < AdjCatlist.Count; i++)
              {
                  if (AdjCatlist[i].ItemId == id)
                  {
                      switch (cTypeId)
                      {
                          case "rbDamage":
                              AdjCatlist[i].Reason = updatedata;
                              break;
                          case "rbGet":
                              AdjCatlist[i].Reason = updatedata;
                              break;
                          case "tbQty":
                              AdjCatlist[i].AdjQty = Convert.ToInt32(updatedata);
                              break;
                      }


                  }
              }
     }
              
    /*If user update 'Adjustment Quantity' after added into List  */
    protected void tbQty_TextChanged(object sender, EventArgs e)
    {
                 TextBox tbQty = (TextBox)sender;
                 GridViewRow thisGridViewRow = (GridViewRow)tbQty.Parent.Parent;
                 string id = thisGridViewRow.Cells[1].Text;
                 string updatedata = "";
                 updatedata = tbQty.Text;
                 Edit_AdjustmentList(id, tbQty, updatedata);

     }
    
 
    }

}